package com.mp.approvalreq.util;

import com.mp.approvalreq.entity.Cc;
import com.mp.user.entity.User;
import com.mp.user.service.UserServicePrx;
import com.mp.user.util.UserUtils;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;

import java.util.*;
import java.util.stream.Collectors;

public class CcUtils {

    public static String getCc(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return null;
        }
        List<Integer> list = ccList.stream().map(Cc::getCcId).collect(Collectors.toList());
        return ListHelper.list2string(list);
    }

    public static String getCcCiid(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return null;
        }
        List<Integer> list = ccList.stream().map(Cc::getCcCiid).collect(Collectors.toList());
        return ListHelper.list2string(list);
    }

    public static String getDefaultCc(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return null;
        }
        List<Integer> list = ccList.stream().filter(cc -> !Objects.equals(cc.getIsTemp(), 1)).map(Cc::getCcId).collect(Collectors.toList());
        return ListHelper.list2string(list);
    }

    public static String getTempCc(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return null;
        }
        List<Integer> list = ccList.stream().filter(cc -> cc.getIsTemp() == 1).map(Cc::getCcId).collect(Collectors.toList());
        if (Utils.isEmpty(list)) {
            return null;
        }
        return ListHelper.list2string(list);
    }

    public static String getArchived(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return null;
        }
        List<Integer> list = ccList.stream().filter(cc -> cc.getFiledStatus() == Cc.HAD_FILED).map(Cc::getCcId).collect(Collectors.toList());
        if (Utils.isEmpty(list)) {
            return null;
        }
        return ListHelper.list2string(list);
    }

    public static boolean isExistUserCc(List<Cc> ccList, User user) {
        return isExistUserCc(ccList, user.getCompanyInfoId());
    }

    public static boolean isExistUserCc(List<Cc> ccList, Integer companyInfoId) {
        if (Utils.isEmpty(ccList)) {
            return false;
        }
        return ccList.stream().anyMatch(cc -> Objects.equals(cc.getCcCiid(), companyInfoId));
    }

    public static Cc getCcByCompanyInfoId(List<Cc> ccList, Integer companyInfoId) {
        return ccList.stream().filter(o -> Objects.equals(o.getCcCiid(), companyInfoId)).findAny().orElse(null);
    }

    /**
     * 是否已全部归档
     *
     * @param ccList
     * @return
     */
    public static boolean isAllFiled(List<Cc> ccList) {
        if (Utils.isEmpty(ccList)) {
            return false;
        }
        return ccList.stream().allMatch(cc -> cc.getFiledStatus() == Cc.HAD_FILED);
    }


    /**
     * 获取一个人的SP上级、DSP副上级 、DM间接上级 、DDM间接副上级   字典
     */
    public static Map<String, List<User>> get4UserSuperior(User user) throws Throwable {
        Map<String, List<User>> map = new HashMap<>();

        UserServicePrx userServicePrx = ServantUtils.getObject(UserServicePrx.class);
        List<User> superiors = userServicePrx.getSuperiorList(user.getContactId());
        // 上级为空（如老板）
        if (Utils.isEmpty(superiors)) {
            return map;
        }

        //获取直属上级 SP
        User sp = superiors.get(0);
        List<User> spList = Collections.singletonList(sp);

        //所有正职上级
        List<User> deptManagers = superiors.stream().filter(s -> UserUtils.isDeptManager(s) || UserUtils.isAdmin(s)).collect(Collectors.toList());
        //获取间接上级 DM  size() > 1 包含过滤老板部门和倒数第二层部门，
        User dm = deptManagers.size() > 1 ? deptManagers.get(1) : null;
        List<User> dmList = Utils.isNull(dm) ? null : Collections.singletonList(dm);

        //过滤出这个人的直接副主管集合 DSP
//        List<User> dspList = superiors.stream().filter(u -> Objects.equals(sp.getDeptId(), u.getDeptId()) && UserUtils.isAssistantManager(u)).collect(Collectors.toList());
        List<User> dspList = getDSuperior(sp, superiors);
        //过滤出这个人的间接副主管集合 DDM
//        List<User> ddmList = superiors.stream().filter(u -> Utils.isNotNull(dm) && Objects.equals(dm.getDeptId(), u.getDeptId()) && UserUtils.isAssistantManager(u)).collect(Collectors.toList());
        List<User> ddmList = getDSuperior(dm, superiors);

        //null 就不存key
        if (Utils.isNotEmpty(spList)) {
            map.put("SP", spList);
        }
        if (Utils.isNotEmpty(dmList)) {
            map.put("DM", dmList);
        }
        if (Utils.isNotEmpty(dspList)) {
            map.put("DSP", dspList);
        }
        if (Utils.isNotEmpty(ddmList)) {
            map.put("DDM", ddmList);
        }
        return map;
    }


    // 传递一个 主管  返回同部门的的副主管     包含老板
    public static List<User> getDSuperior(User user,List<User> superiors){
        if(Objects.isNull(user)){
            return null;
        }
        if (UserUtils.isAdmin(user)){
            return superiors.stream().filter(UserUtils::isAssistantManager).collect(Collectors.toList());
        }
            return superiors.stream().filter(superior -> Objects.equals(user.getDeptId(), superior.getDeptId()) && UserUtils.isAssistantManager(superior)).collect(Collectors.toList());


    }


}
